﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace InControl
{
    public partial class INV_oManage_form : Form
    {
        private ContextMenu contextMenuForRow = new ContextMenu();
        private int rowIndex;
        private int flag;
     

        /// <summary>
        /// 
        /// </summary>
        public INV_oManage_form()
        {
            InitializeComponent();
        }


        /// <summary>
        /// make the item column to the table dynamically
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void item_col_maker(object sender, EventArgs e)
        {
            DataGridViewComboBoxColumn itemColumn = new DataGridViewComboBoxColumn();
            itemColumn.Name = "Item";
            itemColumn.HeaderText = "Item";
            itemColumn.DataPropertyName = "id";
            ((INV_MDIParent)this.MdiParent).appDriver.FillCombo2(itemColumn, "item_table", "name", "id", 0);
            itemColumn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            itemColumn.Resizable = DataGridViewTriState.False;
            itemColumn.Width = 134;
            this.dataGridView1.Columns.Insert(0, itemColumn);
            this.dataGridView1.EditingControlShowing += this.GridView1EditingControlShowing; //Set EditingControlShowing Event of the DataGrid for itemColumn
        }


        /// <summary>
        /// EditingControlShowing Event ocomboBox1.SelectedValue /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GridView1EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 0) //check that itemColumn is in focus
            {
                ComboBox cmbox = (ComboBox)e.Control;
                cmbox.SelectedValue = 0;
                cmbox.SelectionChangeCommitted += new EventHandler(cmbox_SelectionChangeCommitted);
                cmbox.GotFocus += new EventHandler(cmbox_GotFocus);
            }
        }

        private void cmbox_GotFocus(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedValue.ToString() == "0")
                {
                    comboBox1.Focus();
                    throw new Exception("please select employee");
                }
            }
            catch (Exception ex)
            {
                Error_form ef = new Error_form();
                ef.richTextBox1.Text = "An Error Occured: \n" + ex.Message + "\n\n" + ex.StackTrace;
                ef.ShowDialog(this);
            }
        }



        /// <summary>
        /// cmbox_SelectionChangeCommitted Event definition
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                ComboBox t = (ComboBox)sender;
                if (t.SelectedValue.ToString() == "0")
                {
                    t.SelectedIndex = 1;
                    dataGridView1.CurrentRow.Cells["_Quantity"].Value = "0";
                }
                //else
                //{
                    List<String> ls = ((INV_MDIParent)this.MdiParent).appDriver.checkOutGrid(t.SelectedValue.ToString(), 0);
                    dataGridView1.CurrentRow.Cells["_Category"].Value = ls[1];
                    dataGridView1.CurrentRow.Cells["_Type"].Value = ls[2];
                    dataGridView1.CurrentRow.Cells["_Price"].Value = ls[0];
                    dataGridView1.CurrentRow.Cells["_Quantity"].Value = "0";
                    DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

                    dataGridView1.CurrentRow.Cells["_Date"].Value = dt.ToString("G", System.Globalization.DateTimeFormatInfo.InvariantInfo);
                //}
            }
            catch (Exception ex)
            {
                Error_form ef = new Error_form();
                ef.richTextBox1.Text = "An Error Occured: \n" + ex.Message + "\n\n" + ex.StackTrace;
                ef.ShowDialog(this);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void INV_oManage_form_Load(object sender, EventArgs e)
        {
            ((INV_MDIParent)this.MdiParent).appDriver.FillCombo(comboBox1, "person_table", "lname + ', ' + fname + ' ' + mname", "pid", 1); 
            item_col_maker(sender, e);

            // Add context mneu items
            contextMenuForRow.MenuItems.Add("Delete", new EventHandler(label8_Click));
            contextMenuForRow.MenuItems.Add("Edit", new EventHandler(label8_Click));

            DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
            label18.Text = String.Format("{0:d}", dt);

            if (((INV_MDIParent)this.MdiParent).appDriver.group != "administrators")
                dataGridView1.ReadOnly = true;

            panel5.Visible = true; panel6.Visible = true;
         }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (((INV_MDIParent)this.MdiParent).appDriver.group == "administrators")
                {
                    if (e.RowIndex >= 0)
                    {
                        if (!dataGridView1.CurrentRow.IsNewRow)
                        {
                            EditPanel.Visible = true;
                            panel1.Visible = false;
                            rowIndex = e.RowIndex;
                            textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells["_Quantity"].Value.ToString();
                            label11.Text = dataGridView1.Rows[e.RowIndex].Cells["Item"].Value.ToString();
                            textBox7.Enabled = true;
                            textBox7.ReadOnly = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Error_form ef = new Error_form();
                ef.richTextBox1.Text = "ERROR \n\n" + ex.Message;
                ef.ShowDialog(this);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            EditPanel.Visible = false;    
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            EditPanel.Visible = false;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            EditPanel.Visible = false;
            ((INV_MDIParent)this.MdiParent).OpenForm(typeof(INV_office_form));
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label8_Click(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                if (((INV_MDIParent)this.MdiParent).appDriver.group == "administrators")
                {
                    if (comboBox1.SelectedValue.ToString() == "0")
                    {
                        comboBox1.Focus();
                        throw new Exception("please select employee");
                    }
                    EditPanel.Visible = false;
                    int nRowIndex = dataGridView1.Rows.Count;
                    if (nRowIndex > 0)
                    {
                        if (dataGridView1.IsCurrentCellDirty)
                            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

                        //((INV_MDIParent)this.MdiParent).appDriver.CheckOutGridSave(comboBox1.SelectedValue.ToString(), textBox2.ToString(), this.dataGridView1);
                        ((INV_MDIParent)this.MdiParent).appDriver.CheckOutGridSave(comboBox1.SelectedValue.ToString(), this.dataGridView1);
                        label19.Text = ((INV_MDIParent)this.MdiParent).appDriver.GetCheckOuts(comboBox1.SelectedValue.ToString());
                        label3.Text = ((INV_MDIParent)this.MdiParent).appDriver.gridItemTotals(comboBox1.SelectedValue.ToString());   
                    }
                }
            }
            catch (SqlException ex)
            {
                Error_form ef = new Error_form();
                if (ex.Number == 2601)
                {
                    ef.richTextBox1.Text = "error " + ex.Number + "\n resolve duplicate item before checking out \n";
                }
                ef.ShowDialog(this);
            }
            catch (Exception ex)
            {
                Error_form ef = new Error_form();
                ef.richTextBox1.Text = "An Error Occured: \n" + ex.Message + "\n\n" + ex.StackTrace;
                ef.ShowDialog(this);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try { }
            catch (Exception ex)
            {
                Error_form ef = new Error_form();
                ef.richTextBox1.Text = "An Error Occured: " + ex.Message + "\n\n" + ex.StackTrace;
                ef.ShowDialog(this);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //if (comboBox1.SelectedValue.ToString() != "System.Data.DataRowView")
                if (comboBox1.SelectedValue.ToString() != "0")
                {
                    panel5.Visible = false; panel6.Visible = false; 

                    List<TextBox> tl = new List<TextBox>();
                    List<ComboBox> ls = new List<ComboBox>();
                    List<Label> ll = new List<Label>();

                    ll.Add(label16); ll.Add(label17); ll.Add(label3); ll.Add(label19);

                    ((INV_MDIParent)this.MdiParent).appDriver.ComboList(ll, ls, tl, comboBox1.SelectedValue.ToString(), 2);
                    ((INV_MDIParent)this.MdiParent).appDriver.FillTable(dataGridView1, comboBox1.SelectedValue.ToString());
                    dataGridView1.Rows[0].Cells[0].Selected = false;
                    label19.Text = ((INV_MDIParent)this.MdiParent).appDriver.GetCheckOuts(comboBox1.SelectedValue.ToString());
                    label3.Text = ((INV_MDIParent)this.MdiParent).appDriver.gridItemTotals(comboBox1.SelectedValue.ToString());
                    //textBox1.Enabled = false;
                    //textBox3.Enabled = false;
                    //textBox6.Enabled = false;
                    //textBox4.Enabled = false;
                    //textBox2.Enabled = false;
                }
                else
                {
                    label19.Text = ""; label3.Text = ""; label16.Text=""; label17.Text="";
                    panel5.Visible = true; panel6.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Error_form ef = new Error_form();
                ef.richTextBox1.Text = "An Error Occured: " + ex.Message + "\n\n" + ex.StackTrace;
                ef.ShowDialog(this);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox4_Click(object sender, EventArgs e)
        {
            flag = 1;
            panel1.Visible = true;
            textBox7.ReadOnly = true;
            textBox7.Enabled = false;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox5_Click(object sender, EventArgs e)
        {
            flag = 2;
            panel1.Visible = true;
            textBox7.ReadOnly = true;
            textBox7.Enabled = false;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox7_Click(object sender, EventArgs e)
        {
            try
            {
                if (flag == 1)
                {
                    ((INV_MDIParent)this.MdiParent).appDriver.updateGridItem(dataGridView1.Rows[rowIndex].Cells["Item"].Value.ToString(), dataGridView1.Rows[rowIndex].Cells["_Date"].Value.ToString(), textBox7.Text);
                    int scrollPosition = dataGridView1.FirstDisplayedScrollingRowIndex;

                    // Rebind the grid and reset scrolling
                    ((INV_MDIParent)this.MdiParent).appDriver.FillTable(dataGridView1, comboBox1.SelectedValue.ToString());
                    dataGridView1.FirstDisplayedScrollingRowIndex = scrollPosition;
                    dataGridView1.Rows[rowIndex].Cells["_Quantity"].Selected = true;
                    label3.Text = ((INV_MDIParent)this.MdiParent).appDriver.gridItemTotals(comboBox1.SelectedValue.ToString());
                    dataGridView1.Rows[0].Cells[0].Selected = false;
                }
                else
                    if (flag == 2)
                    {
                        ((INV_MDIParent)this.MdiParent).appDriver.DeleteGridItem(dataGridView1.Rows[rowIndex].Cells["Item"].Value.ToString(), dataGridView1.Rows[rowIndex].Cells["_Date"].Value.ToString());
                        dataGridView1.Rows.Remove(dataGridView1.Rows[rowIndex]);
                        label19.Text = ((INV_MDIParent)this.MdiParent).appDriver.GetCheckOuts(comboBox1.SelectedValue.ToString());
                        label3.Text = ((INV_MDIParent)this.MdiParent).appDriver.gridItemTotals(comboBox1.SelectedValue.ToString());
                    }
                EditPanel.Visible = false;
            }
            catch (Exception ex)
            {
                Error_form ef = new Error_form();
                ef.richTextBox1.Text = "An Error Occured: " + ex.Message + "\n\n" + ex.StackTrace;
                ef.ShowDialog(this);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox6_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            textBox7.ReadOnly = false;
            textBox7.Enabled = true;
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void Off_Edit_Panel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void EditPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label8_Click_1(object sender, EventArgs e)
        {

        }
        private void Off_Edit_Panel_Click(object sender, EventArgs e)
        {
            EditPanel.Visible = false;
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            EditPanel.Visible = false;
        }

        private void panel5_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void label19_Click(object sender, EventArgs e)
        {

        }
    }
}
